var app = getApp();

var util=require('../../utils/util.js')
Page({
  data: {
    contact:{},
    address:{},
  },
  onLoad:function(options){
    var userAddress=options.userAddress;
    var contact ={};
    var address ={};
    if (userAddress ){
      userAddress=JSON.parse(userAddress);
      if (userAddress.id){
        contact = { contact: userAddress.contact, phone: userAddress.phone, id: userAddress.id ,sex:userAddress.sex};
        address = userAddress.address;
        address.location=null; 
      }
      
    }else{
      contact={sex:0}
    }
  this.setData({
    contact: contact,
    address: address
  })
  },
  getMapAddress: function () {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        that.setData({
          "address.mapAddress": res.address,
          "address.latitude": res.latitude,
          "address.longitude": res.longitude
        })
      },
    })
  },
  setContact:function(e){
    this.setData({
      "contact.contact":e.detail.value
    })
  },
  setPhone: function (e) {
    this.setData({
      "contact.phone": e.detail.value
    })
  },
  setAddress:function(e){
    this.setData({
      "address.address":e.detail.value
    })
  },
  setSex:function(e){
    this.setData({
      "contact.sex": e.detail.value
    })
  },
  validate:function(){
    var contact=this.data.contact;
    if (!contact.contact || contact.contact == '') { 
      util.showinfo("请填写收货人姓名");
      return fasle;
    }
    if (!contact.phone || contact.phone == '') {
      util.showinfo("请填写收货人手机");
      return fasle;
    }
    var address=this.data.address;
    if (!address.mapAddress || address.mapAddress==''){
      util.showinfo("请点击选择地址");
      return false;
    }
    if (!address.address || address.address == '') {
      util.showinfo("请填写具体地址");
      return false;
    }
    return true;
  },
  bindSubmit:function(){
    var ret=this.validate();
    if (!ret) return ;
    var data=this.data.contact;
    
    data.address=this.data.address;
    var url=util.url.user_address_add;

    util.fetchPost(
      url,
      data,
      function(ret){
        wx.navigateBack();
      }
    )
    
  }
})